package com.example.linyiquan.app.activity;

import android.content.Intent;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import com.example.linyiquan.app.R;
import com.example.linyiquan.app.adapter.GuideAdapter;

import java.util.ArrayList;
import java.util.List;

public class GuideActivity extends AppCompatActivity implements ViewPager.OnPageChangeListener {

    private ViewPager viewPager;
    private GuideAdapter guideAdapter;
    private List<View> views;

    private Button enter;

    private ImageView[] dots;
    private int[] ids = {R.id.lv1, R.id.lv2, R.id.lv3};


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.guide);

        initView();
        initDots();
    }


    /**
     * 添加视图
     */
    private void initView() {
        final LayoutInflater inflater = LayoutInflater.from(this);

        views = new ArrayList<>();
        views.add(inflater.inflate(R.layout.one, null));
        views.add(inflater.inflate(R.layout.two, null));
        views.add(inflater.inflate(R.layout.there, null));

        guideAdapter = new GuideAdapter(views, this);
        viewPager = (ViewPager) findViewById(R.id.viewpager);
        viewPager.setAdapter(guideAdapter);
        viewPager.setOnPageChangeListener(this);

        enter = (Button) views.get(2).findViewById(R.id.enter);
        enter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(GuideActivity.this, LoginActivity.class);
                startActivity(intent);
                finish();
            }
        });
    }

    /**
     * 对底部小圆点进行添加控件
     */
    private void initDots() {
        dots = new ImageView[views.size()];
        for (int i = 0; i < views.size(); i++) {
            dots[i] = (ImageView) findViewById(ids[i]);
        }
    }


    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    /**
     * 对小圆点进行页面判断
     *
     * @param position
     */
    @Override
    public void onPageSelected(int position) {
        for (int i = 0; i < ids.length; i++) {
            if (position == i) {
                dots[i].setImageResource(R.drawable.imgres);
            } else {
                dots[i].setImageResource(R.drawable.imgrestwo);
            }
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
}
